package nx2.arbitrators;

import nx2.behaviors.NxBehavior;
import java.util.Vector;

/**
 * Base-class for all Arbiters. An arbiter should also behave as a Behavior, in
 * order to create complex behaviors. That is why it extends NxBehavior.
 *
 * @author Alejandro Pustowka, Jonathan Hernandez
 */
public abstract class Arbitrator extends NxBehavior{

    /**
     * Returned value when calling remove() function was successfully executed.
     */
    static public final int REMOVE_SUCCESS = 0;

    /**
     * Returned value when calling remove() function was not successful.
     */
    static public final int REMOVE_FAILED = 1;

    /**
     * Vector of NxBehaviors
     */
    protected Vector behaviors;


    /**
     * Simple constructor
     * @param name Name of the arbiter.
     * @param behaviors The behaviors associated to this Arbitrator.
     */
    public Arbitrator(String name) {
        super(name);
        behaviors = new Vector();
    }

    /**
     * Get the associated behaviors of this arbiter (behavior stack).
     * @return Array of NxBehaviors.
     */
    public NxBehavior[] getBehaviors() {
        NxBehavior[] b = new NxBehavior[behaviors.size()];
        return (NxBehavior[])behaviors.toArray(b);
    }

    /**
     * Add a new behavior to the behaviors stack.
     *
     * @param behavior The new NxBehavior to be attached.
     */
    public void add(NxBehavior behavior)
    {
        behaviors.add(behavior);
    }

    /**
     * Remove a behavior based on its name.
     * @param name of the behavior to be removed.
     * @return REMOVE_SUCCESS if the behavior was identified and correctly removed.
     * REMOVE_FAILED when the behavior could not be identified.s
     */
    public int remove(String name)
    {
        //TODO: Probablemente en vez de utilizar remove(index) se puede utilizar
        //remove(Object)
        for(int i=0; i<behaviors.size();i++)
        {
            NxBehavior b = (NxBehavior)behaviors.elementAt(i);
            if(name.equals(b.getName()))
            {
                behaviors.remove(i);
                return this.REMOVE_SUCCESS;
            }


        }

        return this.REMOVE_FAILED;
    }

}
